package yolorpc.server.tcp;


import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import io.vertx.core.parsetools.RecordParser;
import lombok.extern.slf4j.Slf4j;
import yolorpc.server.HttpServer;

/**
 * @author yolo
 * @Program RPC
 * @description tcp服务器
 * @create 2025-05-07 10:24
 **/
@Slf4j
public class VertxTcpServer implements HttpServer {

    @Override
    public void doStart(int port) {
        // 创建 Vert.x 实例
        log.info("======创建vertx实例======");
        Vertx vertx = Vertx.vertx();

        // 创建 TCP 服务器
        log.info("======创建TCP服务器======");
        NetServer server = vertx.createNetServer();

        // 处理请求
        log.info("======开始处理请求======");
        server.connectHandler(new TcpServerHandler());


        // 启动 TCP 服务器并监听指定端口
        server.listen(port, result -> {
            log.info("======启动 TCP 服务器并监听指定端口:{}======", port);
            if (result.succeeded()) {
                log.info("TCP server started on port {}", port);
            } else {
                log.info("Failed to start TCP server: {}", String.valueOf(result.cause()));
            }
        });
    }


    public static void main(String[] args) {
        new VertxTcpServer().doStart(8888);
    }


}
